app.controller('BusRouteModalCtrl', function ($scope, $modalInstance, param, ajax, toaster, $modal, $timeout, $filter) {

	$scope.multiSelected = {};
	$scope.stationFromlist = [];
	$scope.stationTolist = [];
	$scope.scopelist = [
		{ id: 1, name: "市区" },
		{ id: 2, name: "省内" },
		{ id: 3, name: "省际" }
	];
	$scope.param = param;
	if ($scope.param.data == null) $scope.param.data = {};
	$scope.stationlist = $scope.param.list;
	if ($scope.param.data.scope) {
		var name = $scope.param.data.scope;
		for (var i in $scope.scopelist) {
			if ($scope.scopelist[i].name == name) break;
		}
		$scope.scopelist.selected = $scope.scopelist[i];
	} else {
		$scope.scopelist.selected = $scope.scopelist[0];
	}

	// function toDistance(distance) {
	// 	if (distance >= 1000) {
	// 		return distance = (Math.round(distance / 100) / 10).toFixed(1);
	// 	} else if (distance < 1000) {
	// 		return distance = distance / 1000;
	// 	}
	// }

	if ($scope.param.type == "update") {
		ajax.call(new Req.GET({
			resource: 'route/bs/' + $scope.param.data.id + '/station'
		})).then(function (result) {
			$scope.routeStationList = result.result;
			for (var i = 0; i < $scope.routeStationList.length; i++) {
				// $scope.routeStationList[i].distance = toDistance($scope.routeStationList[i].distance);
				$scope.routeStationList[i].duration = $scope.routeStationList[i].duration / 60;
			}
		}, function () { });
		// $scope.param.data.distance = $filter("number")($scope.param.data.distance / 1000, 1);
		$scope.param.data.duration = $filter("number")($scope.param.data.duration / 60, 1);
	}


	(function () {
		ajax.call(new Req.GET({
			resource: 'city/bs/list'
		})).then(function (result) {
			$scope.cityFromlist = result.result;
			$scope.cityTolist = angular.copy(result.result);
		}, function () { });
	})();

	$scope.$watch('cityFromlist.selected', function (newVal, oldVal) {
		if (newVal) {
			newVal = $scope.cityFromlist.selected.id;
			ajax.call(new Req.GET({
				resource: 'station/bs/' + newVal + '/list'
			})).then(function (result) {
				$scope.stationFromlist = result.result;
			}, function () { });
		}
	});

	$scope.$watch('cityTolist.selected', function (newVal, oldVal) {
		if (newVal) {
			newVal = $scope.cityTolist.selected.id;
			ajax.call(new Req.GET({
				resource: 'station/bs/' + newVal + '/list'
			})).then(function (result) {
				$scope.stationTolist = result.result;
			}, function () { });
		}
	});

	//获取组织列表
	$scope.loadOrgList = function () {
		ajax.call(new Req.GET({
			resource: 'organization/3/list'
		})).then(function (result) {
			$scope.orglist = result.result;
			//设置默认值
			if ($scope.param.data.organizationId != null) {
				var id = $scope.param.data.organizationId;
				for (var i in $scope.orglist.length) {
					if ($scope.orglist[i].id == id) break;
				}
				$scope.orglist.selected = $scope.orglist[i];
			} else {
				$scope.orglist.selected = $scope.orglist[0];
			}
		}, function () { });
	};
	$scope.loadOrgList();

	$scope.ids = [];

	$scope.orderby = function () {
		if ($scope.stationFromlist.selected && $scope.stationTolist.selected) {
			if ($scope.multiSelected != undefined && $scope.multiSelected.stationlist.length > 0) {
				for (var i = 0; i < $scope.multiSelected.stationlist.length; i++) {
					if ($scope.multiSelected.stationlist[i].id == $scope.stationFromlist.selected.id ||
						$scope.multiSelected.stationlist[i].id == $scope.stationTolist.selected.id) {
						toaster.pop('warning', '提示', '途径站点不能与起点或终点相同!');
						return;
					}
				}
			}
			$modal.open({
				templateUrl: 'template/bus/route/addSort.html?v=12ba6dcc8a',
				controller: 'BusRouteOrderByCtrl',
				size: 'lg',
				resolve: {
					param: function () {
						return {
							"type": "insert",
							"start": $scope.stationFromlist.selected,
							"termina": $scope.stationTolist.selected,
							"list": $scope.multiSelected.stationlist
						};
					}
				}
			}).result.then(function (param) {
				$scope.ids = param;
				if (param && param.length > 0) {
					var total = 0;
					for (var i in param) total = total + param[i].duration;
					$scope.param.data.duration = total / 60;
				}
			});
		} else {
			toaster.pop('warning', '提示', '请选择起点站点和终点站点！');
			return;
		}
	};

	$scope.ok = function () {
		if ($scope.myForm.$valid) {
			if ($scope.param.type == "insert") {
				if ($scope.ids.length == 0) {
					toaster.pop('info', '提示', '还未进行站点排序，请点击站点排序按钮进行排序!');
					return;
				}
				if ($scope.scopelist.selected == undefined) {
					toaster.pop('warning', '提示', '请选择线路范围!');
					return;
				}
				if ($scope.orglist.selected == undefined) {
					toaster.pop('warning', '提示', '请选择车辆所属单位!');
					return;
				}
				// $scope.param.data.distance = $scope.param.data.distance * 1000;
				$scope.param.data.duration = $scope.param.data.duration * 60;
				$scope.param.data.fromAddr = $scope.stationFromlist.selected.address;
				$scope.param.data.fromId = $scope.stationFromlist.selected.id;
				$scope.param.data.toAddr = $scope.stationTolist.selected.address;
				$scope.param.data.toId = $scope.stationTolist.selected.id;
				$scope.param.data.fromCity = $scope.cityFromlist.selected.name;
				$scope.param.data.fromCityId = $scope.cityFromlist.selected.id;
				$scope.param.data.toCity = $scope.cityTolist.selected.name;
				$scope.param.data.toCityId = $scope.cityTolist.selected.id;
				$scope.param.data.scope = $scope.scopelist.selected.name;
				$scope.param.data.organizationId = 0;
				$scope.param.ids = $scope.ids;
				$modalInstance.close($scope.param);
			}
			if ($scope.param.type == "update") {
				if ($scope.scopelist.selected == undefined) {
					toaster.pop('warning', '提示', '请选择线路范围!');
					return;
				}
				if ($scope.orglist.selected == undefined) {
					toaster.pop('warning', '提示', '请选择车辆所属单位!');
					return;
				}
				if ($scope.ids.length != 0) {
					$scope.param.data.fromAddr = $scope.stationFromlist.selected.address;
					$scope.param.data.fromId = $scope.stationFromlist.selected.id;
					$scope.param.data.toAddr = $scope.stationTolist.selected.address;
					$scope.param.data.toId = $scope.stationTolist.selected.id;
					$scope.param.data.fromCity = $scope.cityFromlist.selected.name;
					$scope.param.data.fromCityId = $scope.cityFromlist.selected.id;
					$scope.param.data.toCity = $scope.cityTolist.selected.name;
					$scope.param.data.toCityId = $scope.cityTolist.selected.id;
					$scope.param.ids = $scope.ids;
				}
				// $scope.param.data.distance = $scope.param.data.distance * 1000;
				$scope.param.data.duration = $scope.param.data.duration * 60;
				$scope.param.data.scope = $scope.scopelist.selected.name;
				$scope.param.data.organizationId = 0;
				$modalInstance.close($scope.param);
			}
		}
	};

	$scope.cancel = function () { $modalInstance.dismiss('cancel'); };
});